package com._21cn.fetch.utils;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.utils.FormatContentUtil;
import com._21cn.framework.http.HttpCaller;
import com._21cn.framework.utils.DateUtil;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URL;
import java.util.*;

public class FetchBaidu {

    private static String postUrl = "http://admin.cms.qiaoliba.com/admin/article/article/save.do";

    private static String hotListUrl = "http://top.baidu.com/buzz?b=1&c=513";


    public static List<String> getTopicList() {
        LinkedList<String> list = new LinkedList<String>();
        try {
            Document doc = Jsoup.parse(new URL(hotListUrl).openStream(), "GBK", hotListUrl);
            Elements elements = doc.select(".list-table .list-title");
            for (Element element : elements) {
//                System.out.println(element.attr("abs:href"));
//                System.out.println(element.text());
                list.addFirst(element.attr("abs:href"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static String getFirstArticleUrl( String url ){
//        String url = "https://www.baidu.com/baidu?cl=3&tn=SE_baiduhomet8_jmjb7mjw&rsv_dl=fyb_top&fr=top1000&wd=CBA%D4%D9%CF%D6%D4%A9%B0%B8";
        try {
            HttpCaller httpCaller = HttpCaller.get(url);
            String html = httpCaller.call().getString();
            Document doc = Jsoup.parse(html);
            Elements elements = doc.select(".c-offset .c-row a");
            for (Element element : elements) {
                if(!StringUtil.isEmpty(element.attr("abs:href"))){
//                    System.out.println(element.attr("abs:href"));
//                    System.out.println(element.text());
                    return element.attr("abs:href");
                }
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    public static Article getArticleContent(String url){
//        String url = "http://www.baidu.com/link?url=e43GDb0LIe2GmV9peNEBFmEkavJrR_YnFcoOI14qT8V_c1wvWkmXGf2B_7PhQF_ZNSR_EdfuZ8tJBPzI5G_tkyXIz4OQhncy6hkleAJnigi";
        Article article = new Article();
        HttpCaller httpCaller = HttpCaller.get(url);
        httpCaller.addHeader("Referer","baijiahao.baidu.com");
        httpCaller.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3514.0 Safari/537.36");
        String html = httpCaller.call().getString();
        Document doc = Jsoup.parse(html);
        String title = doc.select(".article-title").text();
        String content = FormatContentUtil.formatContent(doc.select(".article-content").html());
        article.setTitle(title);
        article.setContent(content);
        return article;
    }

    public static Map<String, String> extractPictureListFromContent(String content) {
        String fetUrl = "http://admin.cms.qiaoliba.com/pictureFetch.do?status=1";
        Map resultMap = new HashMap();
        try {
            String sourceUrl;
            JSONObject array;
            if (!StringUtil.isEmpty(content)) {
                Document doc = Jsoup.parse(content);
                Elements imgs = doc.getElementsByTag("img");
                int i = 0;
                String urls = "";
                for (Element img : imgs) {
                    sourceUrl = img.attr("src").trim();
                    urls = urls + ",21cn.com," + sourceUrl;
                }
                if (!StringUtil.isEmpty(urls)) {
                    urls = urls.substring(",21cn.com,".length(), urls.length());
                }
                String json = HttpCaller.post(fetUrl).addParam("url", urls).call().getString();
                System.out.println("json:" + json);
                array = JSONObject.parseObject(json);
                for (Element img : imgs) {
                    sourceUrl = img.attr("src").trim();
                    JSONObject obj = array.getJSONObject(sourceUrl);
                    String url = obj.getString("url");
                    resultMap.put(sourceUrl, url);
                }
            }
        }catch (Exception e){
            System.out.println("=========NO picture========");
        }

        return resultMap;
    }

    public static void main(String[] args) {
        saveAuto();
//        saveLine();
    }

    public static void saveAuto(){
        List<String> list = FetchBaidu.getTopicList();
        for (String s : list){
            String surl = FetchBaidu.getFirstArticleUrl(s);
            Article article = FetchBaidu.getArticleContent(surl);
            String content = article.getContent();
            Map<String, String> urlmap = extractPictureListFromContent(article.getContent());
            if( urlmap.isEmpty()){
                continue;
            }
            for (String url : urlmap.keySet()) {
                if( !StringUtil.isEmpty(urlmap.get(url)) ){
                    content = content.replace(url.replaceAll("&","&amp;"), (CharSequence) urlmap.get(url));
                }
            }
            HttpCaller.post(postUrl).addParam("title", StringUtil.utf82iso(article.getTitle()))
                    .addParam("content", StringUtil.utf82iso(content)).addParam("sourceName", StringUtil.utf82iso("百家号"))
                    .addParam("mainColumnId", "44")
                    .addParam("publishStatus", "2")
                    .addParam("topTimeStr", DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"))
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                    .addHeader("Accept-Encoding", "gzip, deflate")
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("Cookie", "admin_auth_key=xxt:value_admin")
                    .call();
        }
    }

    public static void saveLine() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/article.txt"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        try {
            while ((line = br.readLine()) != null)
                saveArticle(line);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveArticle(String surl){
        Article article = FetchBaidu.getArticleContent(surl);
        String content = article.getContent();
        Map<String, String> urlmap = extractPictureListFromContent(article.getContent());
        if (urlmap.isEmpty()) {
            return;
        }
        for (String url : urlmap.keySet()) {
            if (!StringUtil.isEmpty(urlmap.get(url))) {
                content = content.replace(url.replaceAll("&", "&amp;"), (CharSequence) urlmap.get(url));
            }
        }
        HttpCaller.post(postUrl).addParam("title", StringUtil.utf82iso(article.getTitle()))
                .addParam("content", StringUtil.utf82iso(content)).addParam("sourceName", StringUtil.utf82iso("百家号"))
                .addParam("mainColumnId", "44")
                .addParam("publishStatus", "2")
                .addParam("topTimeStr", DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .addHeader("Accept-Encoding", "gzip, deflate")
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Cookie", "admin_auth_key=xxt:value_admin")
                .call();
    }

}
